package com.bsi.sentinel.rest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName FeignSentinelController
 * @Author LiuSong
 * @Date 2020/7/30 16:15
 * @Version 1.0
 * @Description feign与sentinel搭配使用的方法，可直接替代hystrix
 */
@RequestMapping("/feign")
@RestController
public class FeignSentinelController {

    @Autowired
    private FeignSentinelService feignSentinelService;

    @GetMapping("/demoRest")
    public String demoRest() {
        return feignSentinelService.demoSentinel("sentinel");
    }

    @FeignClient(value = "demo-service", fallback = FeignSentinelCallBackService.class)
    interface FeignSentinelService {

        @PostMapping(value = "/sentinel/demo")
        String demoSentinel(String info);
    }

    @Component
    public static class FeignSentinelCallBackService implements FeignSentinelService {
        @Override
        public String demoSentinel(String info) {
            return "feign调用失败，异常熔断...";
        }
    }
}
